package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarfromTwoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.GetCarForPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.GetCarForPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarForView;
import com.bjesc_b2c.app.appraiset.utils.LocalGroupSearch;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.CarForBean;

/**
 * Created by Jason Chen on 2017/2/22.
 * 车辆来源
 */

public class CarFromActivity extends BaseActivity implements CarForView {
    @Bind(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.ed_text)
    EditText searchEd;
    private CarfromTwoAdapter twoAdapter;
    private List<CarForBean> carForBean = new ArrayList<>();
    private String marketName;
    private Intent intent;
    private String marketId;
    private LinearLayoutManager twoManager;
    private GetCarForPresenter carForPresenter;
    private int type;

    @Override
    protected int getLayout() {
        return R.layout.activity_dealers_list;
    }

    @Override
    protected void initEventAndData() {
        intent = new Intent();
        type = getIntent().getIntExtra("TYPE", 1);
        titleTv.setText("选择车行");
        getList();
        searchEd.addTextChangedListener(textWatcher);
    }

    @Override
    protected void createPresenter() {

    }

    private void initViews() {
        twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_carfrom_two, carForBean, CarfromTwoAdapter.TYPE_FOURE);
        twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        carfromTwoRecycler.setLayoutManager(twoManager);
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoAdapter.setOnItemClickListener(twoListener);
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = twoManager.findFirstVisibleItemPosition();
        }
    };

    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            intent.putExtra("carForName", twoAdapter.getDatas().get(position).getName());
            intent.putExtra("carFofId", twoAdapter.getDatas().get(position).getDealer_id());//"29," +
            if (type == 1) {
                setResult(3, intent);
                finish();
            } else if (type == 2) {
                intent.setClass(CarFromActivity.this, PatrolShopActivity.class);
                startIntent(intent, false);
            } else if (type == 3) {
                intent.setClass(CarFromActivity.this, DealerDetailsActivity.class);
                startIntent(intent, false);
            }
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    public void getList() {
        String marketID = getIntent().getStringExtra("marketId");
        Logger.d("marketId==" + marketId);
        carForPresenter = new GetCarForPresenterImpl(CarFromActivity.this, this);
        carForPresenter.getCarFor(marketID);
    }

    @Override
    public void loadStart() {
        showLoading();
    }

    @Override
    public void loadCompleted() {
        cancelLoading();
    }

    @Override
    public void loadFailure() {
        cancelLoading();
    }

    @Override
    public void getCarForDealer(List<CarForBean> listbean) {

        Collections.sort(listbean, new Comparator<CarForBean>() {
            public int compare(CarForBean carBrandBean1, CarForBean carBrandBean2) {
                if (carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)) {
                    return 1;
                }
                if (carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)) {
                    return 0;
                }
                return -1;
            }
        });
        carForBean = listbean;
        initViews();
        twoAdapter.notifyDataSetChanged();
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            if (s != null && s.length() > 0) {
                List<CarForBean> listG = LocalGroupSearch.searchGroup(s, (ArrayList<CarForBean>) carForBean);
                if (listG != null && listG.size() > 0) {
                    if (twoAdapter != null)
                        twoAdapter.refresh(listG);
                } else {
                    showToast("搜索为空");
                }
            } else {
                if (twoAdapter != null)
                    twoAdapter.refresh(carForBean);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };
}
